<?php
/**
 * 应用系统角色管理
 * @author tangbl
 *
 */
class RoleAction extends CommonAction {
 
    /**
     * 初始化
     */
    public function _initialize(){
    
    	parent::_initialize();
    	//parent::checkTimestamp();
    }
    
    /**
     * 根据对应的条件查询角色
     * @access public
     * @param  int    $id  角色id
     * @return array
     */
    public function find(){
    
    	$id = $this->_get("id","intval",0);
    
    	$model = D("Role");
    	$map   = array("id"=>$id);
    	$data  = $model->where($map)->find();
    
    	$this->success($data);
    }
    
    /**
     * 添加角色
     * @access public
     * @param  int    $company 所属商户
     * @param  string $title   角色名称
     * @param  string $remark  备注说明
     * @return array
     */
    public function add(){
    	
    	$param = array(
    		"after"   => "role_deal_after",
    	);
    	parent::do_add("Role",$param);
    }
    
    /**
     * 修改角色
     * @access public
     * @param  string $title   角色名称
     * @param  string $remark  备注说明
     * @return array
     */
    public function edit(){
    	$param = array(
    		"after"   => "role_deal_after",
    	);
    	parent::do_edit("AdminRole",$param);
    }
    
    /**
     * 查询角色分页
     * @access public
     * @param  int    $company 所属商户
     * @return array
     */
    public function page(){
		
    	$company = $this->_get("company","intval",0);
    	if($company){
    		$map['company'] = $company;
    	}
    	    	
    	$desc    = $this->_get("orderDirection","trim","desc");;
		$order   = $this->_get("orderName","trim","addtime");
		$pageNum = $this->_get("pageNum","intval",C('PAGE_NUMS'));
		$nowPage = $this->_get("p","intval",1);
    	
    	$model = D("Role");
    	$count = $model->where($map)->count();
    	$list  = $model->where($map)->order($order.' '.$desc)
    			 ->page($nowPage.','.$pageNum)->select();
    	
    	$data  = array("count"=>$count, "list"=>$list);
    	$this->success($data);
    }
    
    /**
     * 查询角色分页
     * @access public
     * @param  int    $company 所属商户
     * @return array
     */
    public function index(){
    
    	$company = $this->_get("company","intval",0);
    	if($company){
    		$map['company'] = $company;
    	}
    	 
    	$model = D("Role");
    	$list  = $model->where($map)->select();
    	 
    	$this->success($list);
    }
    
    /**
     * 根据应用系统角色查询对应的节点
     * @access public
     * @param  int    $role 角色id
     * @return array
     */
    public function listByRole(){
    
    	$map['role'] = $this->_get("role","intval",0);
    
    	$model = D("NodeRoleView");
    	$data  = $model->where($map)->select();
    	 
    	$this->success($data);
    }
    
    /**
     * 根据角色对应的节点
     * @access public
     * @param  int    $role 角色id
     * @return array
     */
    public function nodeByRole(){
    
    	$role   = $this->_get("role","intval",0);
    	
    	$module = D("Node");
    	$filter = "remark,addid,addtime,key,value,url,icon";
    	$list   = $module->field($filter,true)->order("sort desc")->select();
    	
    	if($role){
    		
    		$model  = D("NodeRoleView");
    		$map    = array("role"=>$role);
    		$nodes  = $model->where($map)->getField("id", true);
    	}
    	
    	foreach($list as $k=>$v){
    		
    		if($v['pid'] == 0){
    			$v['isopen'] = 1;
    		}else{
    			$v['isopen'] = 0;
    		}
  
    		if($v['permiss']==1 || in_array($v['id'], $nodes)){
    			$v['flag'] = 1;
    		}else{
    			$v['flag'] = 0;
    		}
    		$data[] = $v;
    	}
    	$this->success($data);
    }
    
    /**
     * 根据角色对应的菜单
     * @access public
     * @param  int    $role 角色id
     * @return array
     */
    public function menuByRole(){
    
    	$role   = $this->_get("role","intval",0);
    	 
    	$module = D("Menu");
    	$filter = "remark,addid,addtime,key,value,url";
    	$list   = $module->field($filter,true)->order("sort desc")->select();
    	 
    	if($role){
    		
    		$map    = array("role"=>$role);
    		$model  = D("MenuRoleView");
    		$menus  = $model->where($map)->getField("id", true);
    		
    		$model  = D("NodeRoleView");
    		$nodes  = $model->where($map)->getField("id", true);
    	}
    	 
    	foreach($list as $k=>$v){
    		
    		$v['checked'] = 0;
    		//菜单没有关联节点;菜单关联节点具有权限
    		$nodeid = $v["nodeid"];
    		
    		if(!$nodeid || in_array($nodeid, $nodes)){
    			$v['disabled'] = 0;
    			
    			if($v['permiss']==1 || in_array($v['id'], $menus)){
    				$v['checked'] = 1;
    			}
    		}else{
    			$v['disabled'] = 1;
    		}

    		$data[] = $v;
    	}
    	$this->success($data);
    }
    
    /**
     * 
     */
    public function menuEdit(){
    	
    	$param = array(
    		"after"   => "role_menu_after",
    	);
    	parent::do_edit("Role",$param);
    }
  
    /**
     * 删除角色
     * @access public
     * @param  int    $id  角色编号
     * @return array
     */
    public function del(){
    	
    	$id     = $this->_get("id","intval",0);
    	
    	$model  = D("Role");
    	$role   = $model->find($id);
    	
    	if($role['status'] == 2){
    		
    		$this->error("系统角色不能删除");
    	}else{
    		
    		$module = M("Admin");
    		$map    = array("role"=>$id);
    		$rows   = $module->where($map)->find();
    		 
    		if($rows['id']){
    			$this->error("角色下存在用户,不能删除");
    		}else{
    		
    			$res = $model->delete($id);
    			if($res){
    				$this->success($res,"删除角色成功");
    			}else{
    				$this->error("删除角色失败");
    			}
    		}
    	}
    }
    
}